package com.net.oil.car;

import android.content.Intent;
import android.os.Bundle;

import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.navigation.NavigationView;
import com.net.oil.R;
import com.net.oil.app.Constant;
import com.net.oil.base.BaseActivity;
import com.net.oil.car.adapter.CarSeriesAdapter;
import com.net.oil.car.adapter.CarTypeAdapter;
import com.net.oil.car.bean.CarSeriesBean;
import com.net.oil.car.bean.CarTypeBean;
import com.net.oil.car.contract.CarSeriesContract;
import com.net.oil.car.presenter.CarSeriesPresenter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CarSeriesActivity extends BaseActivity<CarSeriesPresenter> implements CarSeriesContract.View {

    @BindView(R.id.toolbar_go_back)
    ImageView toolbarGoBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.car_series_list)
    RecyclerView carSeriesList;
    @BindView(R.id.car_type_list)
    RecyclerView carTypeList;
    @BindView(R.id.navigation_view)
    NavigationView navigationView;
    @BindView(R.id.drawer_layout)
    DrawerLayout drawerLayout;

    private CarSeriesAdapter carSeriesAdapter;
    private CarTypeAdapter carTypeAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_car_series);
        ButterKnife.bind(this);
        presenter = new CarSeriesPresenter(this);
        initView();
        initData();
    }

    private void initData() {
        int brandId = getIntent().getIntExtra(Constant.INTENT_CAR_BRAND_ID, -1);
        if (brandId != -1){
            showProgressDialog();
            presenter.getCarSeriesList(brandId);
        }
    }

    private void initView() {
        toolbarTitle.setText("选择车型");
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

        LinearLayoutManager carSeriesManager = new LinearLayoutManager(this);
        carSeriesAdapter =  new CarSeriesAdapter(this);
        carSeriesList.setLayoutManager(carSeriesManager);
        carSeriesList.addItemDecoration(new DividerItemDecoration(this, carSeriesManager.getOrientation()));
        carSeriesList.setAdapter(carSeriesAdapter);
        carSeriesAdapter.setOnCarSeriesItemClickListener(new CarSeriesAdapter.OnCarSeriesItemClickListener() {
            @Override
            public void onClick(int seriesId) {
                showProgressDialog();
                presenter.getCarTypeList(seriesId);
            }
        });


        LinearLayoutManager carTypeManager = new LinearLayoutManager(this);
        carTypeAdapter = new CarTypeAdapter(this);
        carTypeList.setLayoutManager(carTypeManager);
        carTypeList.addItemDecoration(new DividerItemDecoration(this, carSeriesManager.getOrientation()));
        carTypeList.setAdapter(carTypeAdapter);
        carTypeAdapter.setOnCarTypeItemClickListener(new CarTypeAdapter.OnCarTypeItemClickListener() {
            @Override
            public void onClick(CarTypeBean carTypeBean) {

                Intent intent = new Intent(CarSeriesActivity.this, AddCarNumActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable(AddCarNumActivity.CAR_TYPE_BEAN, carTypeBean);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }
        });

    }

    @OnClick(R.id.toolbar_go_back)
    public void onViewClicked() {
        finish();
    }

    /**
     * 获取车系列表成功
     * @param carSeriesBeans    车系列表数据
     */
    @Override
    public void getCarSeriesListSuccess(List<CarSeriesBean> carSeriesBeans) {
        if (carSeriesBeans != null){
            dismissProgressDialog();
            carSeriesAdapter.updateData(carSeriesBeans);
        }
    }

    /**
     * 获取车系列表失败
     */
    @Override
    public void getCarSeriesListFailed() {
        dismissProgressDialog();
    }

    /**
     * 获取车型列表成功
     * @param carTypeBeans  车型列表
     */
    @Override
    public void getCarTypeListSuccess(List<CarTypeBean> carTypeBeans) {
        dismissProgressDialog();
        if (carTypeBeans != null){
            carTypeAdapter.updateData(carTypeBeans);
            drawerLayout.openDrawer(navigationView);
        }
    }

    /**
     * 获取车型列表失败
     */
    @Override
    public void getCarTypeListFailed() {
        dismissProgressDialog();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (drawerLayout.isDrawerOpen(navigationView)){
            drawerLayout.closeDrawers();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
